package com.jie.deletefile.Storage;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.jie.deletefile.FileComent.FileInfo;
import com.jie.deletefile.FileComent.FileManager;
import com.jie.deletefile.FileComent.FileUtils;
import com.jie.deletefile.Permission.CheckPermission;
import com.jie.deletefile.Permission.PermissionActivity;
import com.jie.deletefile.R;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, FileManager.SearchFileListener {

    private android.widget.ListView lv;
    private android.widget.RelativeLayout activitymain;
    MyAdapter adapter;
    private List<FileInfo> data;
    private FileManager fileManager;
    private Thread mThread;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Log.e("TAG", "handleMessage: 2222222222");
                pb.setVisibility(View.GONE);

                data= fileManager.getAnyFileList();
                Log.e("TAG", "handleMessage: data"+data.size() );
                adapter = new MyAdapter(MainActivity.this, R.layout.item, data);
                lv.setAdapter(adapter);
            }
        }
    };
    private android.widget.ProgressBar pb;

    private static final int REQUEST_CODE = 0;
    private CheckPermission checkPermission;
    static final String PERMISSION[] = new String[]
            {Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,};
    private String TAG = "TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission = new CheckPermission(this);
        this.pb = (ProgressBar) findViewById(R.id.pb);
        this.activitymain = (RelativeLayout) findViewById(R.id.activity_main);
        this.lv = (ListView) findViewById(R.id.lv);



        fileManager = FileManager.getFileManager();

       initdata();
        lv.setOnItemClickListener(this);
        if (checkPermission.permissionSet(PERMISSION)) {
            startPermissionActivity();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initdata() {
        fileManager.setSearchFileListener(this);
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                fileManager.searchSDCradFile();

            }
        });
        mThread.start();
    }



    private void startPermissionActivity() {
        PermissionActivity.startActivityForResult(this, REQUEST_CODE, PERMISSION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && requestCode == PermissionActivity.PERMISSION_DENIEG) {
            finish();

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        File file = data.get(position).getFile();
        String path = file.getAbsolutePath();
        boolean b = FileUtils.deleteFile(path);
        Log.e("TAG", "onItemClick: isdelete"+b);
        data.remove(data.get(position));
        adapter.notifyDataSetInvalidated();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void searching(String typeName) {

    }

    @Override
    public void end(boolean isExceptionEnd) {
        Log.e("TAG", "end:aaaaaaaaaaaa");

        handler.sendEmptyMessage(1);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart: 111111111111111111111");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: 222222222222222222222");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: 4444444444444" );
    }
}
